<?php
namespace Home\Controller;
use Think\Cache\Driver\Redis;
class LinuxController extends CommonController {

//定时任务接口
public function index(){
	while (true) {
		$this->order_filter();
		sleep(1);
	}
}
public function nobody(){
	while (true) {
		$this->get_order();
		sleep(2);
	}
}

public function outtime(){
	while (true) {
		$this->order_outtime();
		sleep(2);
	}
}
public function signorder(){
	while (true) {
		$this->signorder_filter();
		sleep(2);
	}
}
/*public function re_order(){
	while (true) {
		$this->order_reorder();
		sleep(2);
	}
}*/
public function timing(){
	while (true) {
		$this->ordertiming();
		sleep(20);
	}
}
public function get_order(){
	$db_order = M('order');
	// $db_orderinfo = M('orderinfo');
	$signsystem = M("systemsetup")->where(array("type"=>0))->select();
/*-------------订单----------------*/
	$time = $signsystem['3']['value'];
	$where = "pay_status = 1 and type=1 and status = 1 and sub_type!=3 and actual_join_num=0";
	$order = $db_order->where($where)->find();
	// p($order);exit();
	if ($order &&  time()>($order['pay_time']+$order['snatch_time']+5)) {
		echo "|%".date("H:i:s",time())."nobody:".$order['id'];
		$result = $db_order->where(array('id'=>$order['id']))->save(array('status'=>11));
		// $this->postsocket($order['id']);
		if ($result && $order['sub_type'] == 0) {
			$this->gettheorder($order,$signsystem);
		}
		echo "nobody%|";
	}
}
public function order_filter(){
	$db_order = M('order');
	// $db_orderverify = M('orderverify o');
	// $signsystem = M("systemsetup")->where(array("type"=>0))->select();
/*-------------订单----------------*/
	// $time = $signsystem['3']['value'];
	$where = "pay_status = 1 and sub_type!=3 and status = 1 and actual_join_num !=0 and type=1";
	$order = $db_order->where($where)->find();
	// p($order);//exit();
	// foreach ($order as $key => $value) {
		if ($order['id'] && time()>($order['pay_time']+$order['snatch_time'])) {
			echo "|%".date("H:i:s",time())."filter:".$order['id'];
			$start = microtime(true);
			$this->allocation_order($order);
			$end = microtime(true);
			// echo "@|".date("i:s",time())."=".round($end-$start,3)."|@";
			echo "filter%|";
		}
	// }

// 距离交单20分钟
	$remind_order = $db_order->where("(type=1 or type=2) and pay_status=1 and sub_type!=1 and status=3 and isremind=0 and unix_timestamp(now())>(pay_time+deliver_time+delay_time-1200)")->field('id,user_id,type')->select();
	if ($remind_order) {
		foreach ($remind_order as $key => $value) {
			$orderdata = $db_order->where(array("id"=>$value['id']))->find();
			A("Template")->ordersendmessage($value['user_id'],2,$orderdata,$data);
			// $res = $this->ordersendmessage($value['user_id'],2,0,0,$value['id'],0,"",$value['type']);
			$db_order->where(array("id"=>$value['id'],"user_id"=>$value['user_id']))->save(array("isremind"=>1));
		}
	}

}
public function signorder_filter(){
	$db_order = M('order');
	$signsystem = M("systemsetup")->where(array("type"=>0))->select();
/*-------------订单----------------*/
	// $time = $signsystem['18']['value'];
	$where = "type=2 and pay_status=1 and sub_type!=3 and status=1 and unix_timestamp(now())>(pay_time+snatch_time)";
	$order = $db_order->where($where)->select();
	// p($order);exit();
	foreach ($order as $key => $value) {
		$o['status'] = 11;
		$db_order->where(array('id'=>$value['id']))->save($o);
		$order_array = json_decode(RedisGet("order".$value['id']),TRUE);
		$user = array_keys($order_array['list'])['0'];
		$ordertime = $value['pay_time']+$value['snatch_time'];
		$time = date("m-d H:i",$ordertime);
		$tel = $value['media_tel'];//M('media')->field('media_tel')->where(array('id'=>$value['mid']))->find()['media_tel'];
		// $res = $this->nobabyAlidayu("15988861092","SMS_94740067",$time,$tel);
		// $res = $this->nobabyAlidayu("13810163665","SMS_94740067",$time,$tel);
		// $result = $this->postsocket($value['id']);
		$title = "签约订单未响应通知";
		$userdata = M('member')->where(array('uuid'=>$user))->field("nickname")->find();
		$media_name = $value['media_name'];//M('Media')->where(array('id'=>$value['mid']))->field("media_name")->find();
		$content = $media_name."标题为《".$value['title']."》的签约订单 ".$user."号主播 ".$userdata['nickname']."未响应,请手动转单";
		$this->noticeemail($title,$content,10,$value['id']);
	}
}
function order_outtime(){
	$signsystem = M("systemsetup")->where(array("type"=>0))->select();
	$where = "(type=1 or type=2) and pay_status=1 and sub_type!=1 and sub_type!=3 and (t.status=3 or t.status=9) and unix_timestamp(now())>(pay_time+deliver_time+delay_time)";
	$vo = M('order t')
						// ->join('__ORDERVERIFY__ v ON t.id = v.oid')
						->join('__MEMBER__ u ON t.user_id = u.uuid')
						->where($where)
						->field('t.*,u.username,u.nickname')
						->find();
						// p($vo);exit();
	// foreach($data as $vo){
	if ($vo) {
		echo "|%".date("H:i:s",time())."outtime:".$vo['id'];
		M('order')->where(array('id'=>$vo['id']))->save(array('status'=>13));
		// M('orderverify')->where(array('oid'=>$vo['id']))->save(array('successful'=>2));
		$dataList = array('uuid'=>$vo['user_id'],'score'=>'10','time'=>time(),'describe'=>'未按时交付订单');
		M('member')->where(array("uuid"=>$vo['user_id']))->setDec('score',10);
		M('score')->add($dataList);
		$data['scorenum'] = 10;
		A("Template")->ordersendmessage($vo['user_id'],5,$vo,$data);
		// 推送
		$socketdata['type'] = "order_outtime";
		$socketdata['id'] = $vo['id'];
		$socket = new \Home\Controller\SocketController();
		$socket->send_to_uid($vo['user_id'],$socketdata);
		// $this->postsocket($vo['id']);
		$this->UpdateSendGetOrder($vo['user_id']);

		// 发短信
		$tel = $vo['media_tel'];
		// $res1 = $this->sendAlidayu($tel,"SMS_94705055","闪电配音");
		$tel1 = $vo['username'];
		$time = $vo['pay_time']+$vo['deliver_time']+$vo['delay_time'];
		$time = date("m-d H:i",$time);
		$num = $vo['id'];
		// $res2 = $this->outtimeAlidayu("15988861092","SMS_95565059",$num,$time,$tel1,$tel);
		// $res2 = $this->outtimeAlidayu("13810163665","SMS_95565059",$num,$time,$tel1,$tel);


		if ($vo['type'] == 1) {
			$title = "订单超时未完成通知";
			$content = $vo['media_name']."标题为《".$vo['title']."》的订单 ".$vo['user_id']."号主播 ".$vo['nickname']."超时未完成,系统将重新配单";
			$this->noticeemail($title,$content,9,$vo['id']);
			$this->getthetimeoutorder($vo,$signsystem);
		}else{
			$title = "签约订单超时未完成通知";
			$content = $vo['media_name']."标题为《".$vo['title']."》的订单 ".$vo['user_id']."号主播 ".$vo['nickname']."超时未完成,请手动转单";
			$this->noticeemail($title,$content,10,$vo['id']);
		}
		echo "outtime%|";
	// }
	}
}

/*public function order_reorder(){
	$db_theorder = M('theorder');
	$db_member = M('member');
	$signsystem = M("systemsetup")->where(array("type"=>0))->select();

	$where = "isreorder = 0 and state = 8 and attribute=0";
	$reorder = $db_theorder->where($where)->find();
	// foreach ($reorder as $key => $reorder) {
	if ($reorder) {
		echo "|%".date("H:i:s",time())."reorder:".$reorder['id'];
		$db_theorder->where(array("id"=>$reorder['id']))->save(array("state"=>8,"isreorder"=>1));
		M('income')->where(array("oid"=>$reorder['id'],"uuid"=>$reorder['uuid']))->save(array("status"=>2));
		$fail = 3;
		$number = 5;
		$db_member->where(array("uuid"=>$reorder['uuid']))->setDec('score',$number);
		M('score')->add(array("uuid"=>$reorder['uuid'],"score"=>$number,"time"=>time(),"describe"=>"改派订单扣分"));
		$res = $this->ordersendmessage($reorder['uuid'],1,$number);
		$this->UpdateSendGetOrder($reorder['uuid']);
		$title = "订单取消通知";
		$userdata = M('member')->where(array('uuid'=>$reorder['uuid']))->field("nickname")->find();
		$medisinfo = M('Media')->where(array('id'=>$reorder['mid']))->field("media_name")->find();
		$content = $medisinfo['media_name']."标题为《".$reorder['title']."》的订单 ".$reorder['uuid']."号主播 ".$userdata['nickname']."取消,系统将重新配单";
		M('noticeemail')->add(array("title"=>$title,"content"=>$content,"type"=>1));
		$this->gettheorder($reorder,$signsystem,$fail);
		echo "-".date("H:i:s",time())."reorder:".$reorder['id']."%|";
	}
	// }

}*/
	public function reorder()
	{
		// $_POST['uuid'] = "MuTuAiwdMDA4";
		// $_POST['oid'] = "NhDeIaO0O0Or";
		// echo urlsafe_b64encode_uuid(100008)."|".urlsafe_b64encode(42);exit();
		// 1.接受参数
		if (!$_POST){$this->ApiError('仅支持POST请求', '999999');}
		$uuid = !empty(I('post.uuid')) ? (I('post.uuid')) : $this->ApiError('uuid不能为空','900001');
		$oid = !empty(I('post.oid')) ? (I('post.oid')) : $this->ApiError('oid不能为空','900002');
		// 2.通过uuid验证用户基本信息
		$Member = D('Member');
		$Verifyuuid = $Member->Verifyuuid($uuid);
		if($Verifyuuid == false){$this->ApiError('用户不存在','900003');}
		$db_order = D('Order');
		$orderdata = $db_order->Verifyorderdata($oid);
		if(!$orderdata){$this->ApiError('订单不存在','900004');}
		// $Verifystate = $db_order->Verifystate($oid);
		// if(!$Verifystate){$this->ApiError('订单已取消','900005');}

		// 3.通过code获取用户信息
		// $db_order = M('theorder');
		// $db_orderinfo = M('theorderinfo');
		// $db_order = $db_order->where("id = $oid and state != 8")->field('id,mid,state,title,uuid,attribute')->find();
		if ($orderdata['sub_type']==3) {
			$this->ApiError('自选订单不可取消','900005');
		}
		if ($orderdata['requirement']==3) {
			$this->ApiError('加急订单不可取消','900005');
		}
		if ($orderdata['type']==3) {
			$this->ApiError('派发订单不可取消','900005');
		}
		if ($orderdata['status']==15) {
			$this->ApiError('订单已取消','900005');
		}
		if ($orderdata['status']==5) {
			$this->ApiError('订单已完成，不可操作','900006');
		}
		if ($orderdata['status'] == 3 && time()>$orderdata['operating_time']+600) {
			$this->ApiError('改派已超时','900007');
		}
		if ($orderdata['status'] == 1) {
			$number = 2;
		}else{
			$number = 5;
		}
		$user = $Member->field('score')->where(array("uuid"=>$uuid))->find();
		$user['number'] = $number;
		$user['score'] = $user['score']-$number;

		$res = $db_order->where(array("id"=>$oid))->save(array("status"=>15,"delay_type"=>3));
		if ($res) {
			// $db_orderinfo->where(array("uuid"=>$uuid,"oid"=>$oid))->save(array("status"=>6));
			// M('orderverify')->where(array('oid'=>$oid))->save(array('successful'=>3));
			$Member->where(array("uuid"=>$uuid))->setDec('score',$number);
			M('score')->add(array("uuid"=>$uuid,"score"=>$number,"time"=>time(),"describe"=>"改派订单扣分"));
			$socketdata['type'] = "order_cancle";
			$socketdata['id'] = $oid;
			$socket = new \Home\Controller\SocketController();
			$socket->send_to_uid($uuid,$socketdata);
			$data['scorenum'] = $number;
			A("Template")->ordersendmessage($uuid,1,$orderdata,$data);
			// $this->postsocket($oid);
			$userdata = M('member')->where(array('uuid'=>$uuid))->field("nickname")->find();
			if ($orderdata['type'] == 2) {
				// $attribute = 2;
				$state = 10;
				$title = "签约订单取消通知";
				$content = $orderdata['media_name']."标题为《".$orderdata['title']."》的订单 ".$uuid."号主播 ".$userdata['nickname']."取消,请手动转单";
				// $dyres = $this->signreorderAlidayu("15988861092","SMS_118725135",$orderdata['title'],$uuid);
				// $dyres = $this->signreorderAlidayu("13810163665","SMS_118725135",$orderdata['title'],$uuid);
			}else{
				// $attribute = 1;
				$state = 9;
				// $db_order->where(array("id"=>$oid))->save(array("isreorder"=>1));
				M('income')->where(array("oid"=>$oid,"uuid"=>$uuid))->save(array("status"=>2));
				$title = "订单取消通知";
				$content = $orderdata['media_name']."标题为《".$orderdata['title']."》的订单 ".$uuid."号主播 ".$userdata['nickname']."取消,系统将重新配单";
				$signsystem = M("systemsetup")->where(array("type"=>0))->select();
				ob_start();
				$this->gettheorder($orderdata,$signsystem);
				ob_clean();
			}
			$this->UpdateSendGetOrder($uuid);
			$this->noticeemail($title,$content,$state,$oid);
			// $wxres = $this->ordersendmessage($uuid,1,$number,0,0,0,"",$attribute);
			$this->ApiSuccess("",$user);
		}else{
			$this->ApiError('不可重复操作','900008');
		}
	}
public function ordertiming(){
	$db_ordertiming = M('systemorder');
	$where = "sendstatus = 0";
	$orderlist = $db_ordertiming->where($where)->select();
	if ($orderlist) {
		foreach ($orderlist as $key => &$value) {
			if (time()>$value['ordertiming']) {
				$signsystem = M("systemsetup")->where(array("type"=>0))->select();
				$db_ordertiming->where(array("id"=>$value['id']))->save(array("sendstatus"=>1));
				$this->gettheorder($value,$signsystem);
			}
		}
	}
}

/*-----------------------------*/

}
